var AjaxCrud = QualityCheck.AjaxCRUD;
var serverUrl = QualityCheck.GPUD.ServiceConfig;
$(function(){
	var usercode = $("#user-name",parent.parent.document).attr("code");
	var userkey = $("#user-name",parent.parent.document).attr("key");
	var usertype = $("#user-name",parent.parent.document).attr("usertype");
	keyCheck(usercode,usertype,userkey,function(){
		initSmPages();
		window.onresize = function () {
			initSmPages();
		};
		pageBtns();
		getWorkTime();
	});
});
function addWorktimelist(){
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var obj = {};
	for(var i = 1; i < 25; i ++){
		if($("#worktime" + i + "-1").val() != "" || $("#worktime" + i + "-2").val() != ""){
			obj["start"+i] = $("#worktime" + i + "-1").val();
			obj["stop"+i] = $("#worktime" + i + "-2").val();					
		}else{
			obj["start"+i] = "";
			obj["stop"+i] = "";					
		}
	}
		
	obj.cardcode = imei;
	obj.work = 1;
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var data = {
			cmd:"setCardWork",
			model:"card",
			userKey:userKey,
			data:obj,
			remark:"",
	};
	processBegin("正在应用中···");
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			processEnd("设置成功");
			getWorkTime();
		}else{
			processError("设置失败");
		}
	},null,null);	
	
}
function pageBtns(){
	$(".students-choice",parent.document).unbind("click");
	$(".students-choice",parent.document).click(function(){
		$(".students-choice",parent.document).removeClass("students-choice-select");
		$(this).addClass("students-choice-select");
		getWorkTime();
	});
	$("#worktime-list-set").click(function(){
		addWorktimelist();
	});
}
function getWorkTime(){
	var userKey = $("#user-name",parent.parent.document).attr("key");
	var imei = $(".students-choice-select",parent.document).attr("imei");
	var data = {
			cmd:"getCardWork",
			model:"card",
			userKey:userKey,
			data:{
				cardcode:imei,
				work:1,
			},
			remark:"",
	};
	AjaxCrud.add(serverUrl.ServerLoginUrl,data,function(res){
		if(res.result == 1 && res.data != undefined && res.data != ""){
			var list = res.data;
			for(var i = 1; i < 25; i++){
				for(var key in list[i]){
					if(list[i][key] == null || list[i][key] == "null" ){
						list[i][key] = "";
					}
				}
				if($("#worktime" + i + "-1") && $("#worktime" + i + "-2")){
					$("#worktime" + i + "-1").val(list["start" + i ]);
					$("#worktime" + i + "-2").val(list["stop" + i ]);
				}
			}
		}else{
			for(var i = 1; i < 25; i++){
				$("#worktime" + i + "-1").val("");
				$("#worktime" + i + "-2").val("");
			}
		}
	},null,null);	
}
function initSmPages(){
	var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
	$(".right-content").height(rcH);
	
	var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
	$(".content-data-show").height(cdsH);
}